package com.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.constant.FileUploadConstant;
import com.constant.MqttMessageType;
import com.dao.AppVersionUpgradeMapper;
import com.dao.DeviceRelationAppVersionMapper;
import com.emqx.MqttMessageDTO;
import com.emqx.MqttMessageSender;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.model.AppVersionUpgrade;
import com.model.DeviceRelationAppVersion;
import com.page.GenericPage;
import com.po.AppVersionUpgradeModifyPO;
import com.po.AppVersionUpgradePO;
import com.result.Result;
import com.service.AppVersionUpgradeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AppVersionUpgradeServiceImpl implements AppVersionUpgradeService {

    @Autowired
    AppVersionUpgradeMapper appVersionUpgradeMapper;

    @Autowired
    DeviceRelationAppVersionMapper deviceRelationAppVersionMapper;


    @Autowired
    private MqttMessageSender mqttMessageSender;

    @Override
    public Result<AppVersionUpgrade> save(AppVersionUpgradePO appVersionUpgradePO) {
        if (ObjectUtils.isEmpty(appVersionUpgradePO.getApkVersion())
                || ObjectUtils.isEmpty(appVersionUpgradePO.getPackageName())
                || ObjectUtils.isEmpty(appVersionUpgradePO.getUploadPath())){
            return new Result<>().error("参数异常");
        }
        if (!FileUploadConstant.APK_PACKAGE_NAME.equals(appVersionUpgradePO.getPackageName())){
            return new Result<>().error("软件包名错误");
        }
        AppVersionUpgrade appVersionUpgrade = new AppVersionUpgrade();
        appVersionUpgrade.setApkVersion(appVersionUpgradePO.getApkVersion());
        List<AppVersionUpgrade> list = appVersionUpgradeMapper.selectByCondition(appVersionUpgrade);
        if (list == null || list.size() == 0) {
            BeanUtils.copyProperties(appVersionUpgradePO, appVersionUpgrade);
            Date time = new Date();
            appVersionUpgrade.setCreateTime(time);
            appVersionUpgrade.setUpdateTime(time);
            appVersionUpgrade.setUpdated(appVersionUpgradePO.getCreated());
            appVersionUpgrade.setReleastStatus(1);
            int count = appVersionUpgradeMapper.insertSelective(appVersionUpgrade);
            return new Result<AppVersionUpgrade>().ok(appVersionUpgrade);
        } else {
            return new Result<>().error("版本号已存在");
        }
    }


    @Override
    public Result<AppVersionUpgrade> modifyById(AppVersionUpgradeModifyPO appVersionUpgradeModifyPO) {
        AppVersionUpgrade appVersionUpgrade = new AppVersionUpgrade();
        BeanUtils.copyProperties(appVersionUpgradeModifyPO, appVersionUpgrade);
        appVersionUpgradeMapper.updateByPrimaryKeySelective(appVersionUpgrade);

        if (!ObjectUtils.isEmpty(appVersionUpgradeModifyPO.getReleastStatus())){
            if (appVersionUpgradeModifyPO.getReleastStatus() == 2){
                //3.通知app端
                //发送通知
                JSONObject body = new JSONObject();
                body.put("apk_version",appVersionUpgrade.getApkVersion());
                Date now = new Date();
                MqttMessageDTO message=new MqttMessageDTO();
                message.setMsgSender("server_in");
                message.setMsgReceiver("server_out");
                message.setMsgType(MqttMessageType.CHECK_APK_VERSION);
                message.setMsgBody(body.toJSONString());
                message.setSendTime(now);
                mqttMessageSender.send(message);
            }else if (appVersionUpgradeModifyPO.getReleastStatus() != 3 && appVersionUpgradeModifyPO.getReleastStatus() != 1){
                return new Result<>().error("应用状态参数错误");
            }

        }
        return new Result<AppVersionUpgrade>().ok();
    }

    @Override
    public Result<AppVersionUpgrade> queryById(Long id) {
        AppVersionUpgrade appVersionUpgrade = appVersionUpgradeMapper.selectByPrimaryKey(id);
        return new Result<AppVersionUpgrade>().ok();
    }

    @Override
    public Result<GenericPage<AppVersionUpgrade>> queryAll(int pageNo, int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<AppVersionUpgrade> apkUpdateList = appVersionUpgradeMapper.selectAll();
        PageInfo<AppVersionUpgrade> pageInfo = new PageInfo<>(apkUpdateList);
        GenericPage<AppVersionUpgrade> genericPage = new GenericPage<>(pageSize, pageInfo.getTotal(), apkUpdateList);
        return new Result<GenericPage<AppVersionUpgrade>>().ok(genericPage);
    }


    public Result<AppVersionUpgrade> queryLast(Long deviceId){

        DeviceRelationAppVersion deviceRelationAppVersion = deviceRelationAppVersionMapper.selectByDeviceId(deviceId);

        if (ObjectUtils.isEmpty(deviceRelationAppVersion)){
            AppVersionUpgrade appVersionUpgrade = new AppVersionUpgrade();
            appVersionUpgrade.setApkVersion(0);
            return new Result<AppVersionUpgrade>().ok(appVersionUpgrade);
        }else{
            AppVersionUpgrade appVersionUpgrade = appVersionUpgradeMapper.selectByPrimaryKey(deviceRelationAppVersion.getAppVersionId());
//            AppVersionUpgrade appVersionUpgrade = appVersionUpgradeMapper.selectLast();
            return new Result<AppVersionUpgrade>().ok(appVersionUpgrade);
        }
    }

}
